package com.lz.football.module.activity.liveness
{
	import com.common.utils.Reflection;
	import com.liangzhong.events.ActivityEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.lz.football.module.activity.ILiveness;
	
	import common.BaseView.BaseView;
	import common.BaseView.DisplayMouseEventManager;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	
	public class LivenessView extends BaseView implements ILiveness 
	{
		private var d:Object = {league:{name:"完成一场联赛",need:1,live:10},
			nation:	{name:"完成1场国家挑战赛",need:1,live:10},
			ladder:	{name:"完成一场天梯赛",need:1,live:10},
			explore:{name:"进行5次球员探索！",need:5,live:10},
			orange:	{name:"成功签约一名橙色球员！",need:1,live:20},
			europe:	{name:"赢取一场冠军联赛！",need:1,live:15},
			resolve:{name:"在仓库中分解一件道具",need:1,live:10},
			trikot:	{name:"进行一次球衣合成！",need:1,live:15}};
		
		private var _list:Array = [];
		/**
		 * 进度
		 */
		private var pro:MovieClip;
		/**
		 *效果 
		 */
		private var eff:MovieClip;
		/**
		 *活跃度 
		 */
		private var progress:TextField;
		/**
		 * 
		 */
		private var _itemVec:Vector.<LivenessItem>;
		/**
		 *领取奖励的ID 
		 */
		private var _select:int;
		
		public function LivenessView()
		{
			super();
		}
		override public function initUI(view:Object=null):void
		{
			symbolSkin = Reflection.createInstance("com.lz.res.liveness");
			if(symbolSkin == null)return;
			this.addChild(symbolSkin);
			pro = symbolSkin.pro.pro;
			eff = symbolSkin.pro.eff;
			pro.width = 10;
			eff.x = pro.width - eff.width/2;
//			if(eff.x <= 0)
//				eff.x = 0;
			_windowBg = symbolSkin.livenessBg;
			isDrag = true;
			isCloseBtn = true;
		
			_viewHeight = symbolSkin.height;
			_viewWidth  =symbolSkin.width;
			moveClose(_viewWidth - 35,28);
			onResizeHandler();
			
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.treasure_1,"奖励：欧元10000");
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.treasure_2,"奖励：声望200");
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.treasure_3,"奖励：强力恢复卡");
			DisplayMouseEventManager.instance.addTipsDisplay(symbolSkin.treasure_4,"奖励：幸运卡");
			showInfo();
			
			symbolSkin.treasure_1.filters = filter;
			symbolSkin.treasure_2.filters = filter;
			symbolSkin.treasure_3.filters = filter;
			symbolSkin.treasure_4.filters = filter;
			addEvtListener();
		}
		
		/**
		 *显示信息 
		 * 
		 */
		public function showInfo():void
		{
			symbolSkin.openStatus_1.text = "未开启";
			symbolSkin.openStatus_2.text = "未开启";
			symbolSkin.openStatus_3.text = "未开启";
			symbolSkin.openStatus_4.text = "未开启";
			symbolSkin.progress.text = "0/100";
			_itemVec = new Vector.<LivenessItem>();
			var item:LivenessItem;
			var len:int = _list.length;
			for(var i:int = 0;i < len;i++)
			{
				item = new LivenessItem();
				item.initUI();
				item.setDate(_list[i]);
				item.x = (item.width + 5) * (i % 2) + 25;
				item.y = (item.height + 2) * int(i / 2) + 212;
				this.addChild(item);
			}
		}
		/**
		 *获取滤镜 
		 * @return 
		 * 
		 */		
		private function get filter():Array
		{
			var arr:Array = [new ColorMatrixFilter([0.3086, 0.6094, 0.082, 0, 0,
				0.3086, 0.6094, 0.082, 0, 0,
				0.3086, 0.6094, 0.082, 0, 0,
				0,0,0,1,0,])];
			return arr;
		}
		override public function addEvtListener():void
		{
			symbolSkin.btn_1.addEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_2.addEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_3.addEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_4.addEventListener(MouseEvent.CLICK,onClickHandler);
			
			
		}
		
		/**
		 * 
		 * @param event
		 * 
		 */
		private function onClickHandler(event:MouseEvent):void
		{
			symbolSkin.btn_1.removeEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_2.removeEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_3.removeEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_4.removeEventListener(MouseEvent.CLICK,onClickHandler);
			switch(event.target)
			{
				case symbolSkin.btn_1:
					_select = 1;
					this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_LIVENESS_AWARD,{id:1}));
					break;
				case symbolSkin.btn_2:
					_select = 2;
					this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_LIVENESS_AWARD,{id:2}));
					break;
				case symbolSkin.btn_3:
					_select = 3;
					this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_LIVENESS_AWARD,{id:3}));
					break;
				case symbolSkin.btn_4:
					_select = 4;
					this.dispatchEvent(new ActivityEvent(ActivityEvent.ACTIVITY_LIVENESS_AWARD,{id:4}));
					break;
			}
		}
		override public function removeEvtListener():void
		{
			symbolSkin.btn_1.removeEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_2.removeEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_3.removeEventListener(MouseEvent.CLICK,onClickHandler);
			symbolSkin.btn_4.removeEventListener(MouseEvent.CLICK,onClickHandler);
			DisplayMouseEventManager.instance.removeTipsDisplay(symbolSkin.treasure_1);
			DisplayMouseEventManager.instance.removeTipsDisplay(symbolSkin.treasure_2);
			DisplayMouseEventManager.instance.removeTipsDisplay(symbolSkin.treasure_3);
			DisplayMouseEventManager.instance.removeTipsDisplay(symbolSkin.treasure_4);
			super.removeEvtListener();
		}
		override public function onCloseHandler(event:MouseEvent):void
		{
			this.dispatchEvent(new ActivityEvent(ModuleEvent.REMOVE_MODULE));
		}
		override public function destroyView():void
		{
			removeEvtListener();
			super.destroyView();
		}
		/**
		 *活跃度信息 
		 * @param $obj
		 * 
		 */
		public function livenessInfo($obj:Object):void
		{
			addElement($obj.league,d.league);
			addElement($obj.nation,d.nation);
			addElement($obj.ladder,d.ladder);
			addElement($obj.explore,d.explore);
			addElement($obj.orange,d.orange);
			addElement($obj.europe,d.europe);
			addElement($obj.resolve,d.resolve);
			addElement($obj.trikot,d.trikot);
			showInfo();
			symbolSkin.progress.text = $obj.vitality + "/100";
			
			pro.width = $obj.vitality / 100 * 470;
			if(pro.width <= 0)
				pro.width = 10;
			eff.x = pro.width - eff.width/2;
			if($obj.status1 == 1)//领取奖励0.未领取，1.已领取
			{
				setBoxStatus(1,1);
			}else
			{
				if($obj.vitality >= 20)//完成
				{
					setBoxStatus(2,1);
				}else//未完成
				{
					setBoxStatus(3,1);
					symbolSkin["openStatus_" + 1].text = "活跃度20开启";
				}
			}
			if($obj.status2 == 1)//领取奖励
			{
				setBoxStatus(1,2);
			}else
			{
				if($obj.vitality >= 50)
				{
					setBoxStatus(2,2);
				}else
				{
					setBoxStatus(3,2);
					symbolSkin["openStatus_" + 2].text = "活跃度50开启";
				}
			}
			if($obj.status3 == 1)//领取奖励
			{
				setBoxStatus(1,3);
			}else
			{
				if($obj.vitality >= 80)
				{
					setBoxStatus(2,3);
				}else
				{
					setBoxStatus(3,3);
					symbolSkin["openStatus_" + 3].text = "活跃度80开启";
				}
			}
			if($obj.status4 == 1)//领取奖励
			{
				setBoxStatus(1,4);
			}else
			{
				if($obj.vitality >= 100)
				{
					setBoxStatus(2,4);
				}else
				{
					setBoxStatus(3,4);
					symbolSkin["openStatus_" + 4].text = "活跃度100开启";
				}
			}
		}
		/**
		 *领取奖励返回 
		 * 
		 */
		public function award($obj:Object):void
		{
			addEvtListener();
			if(!$obj.status)return;
			setBoxStatus(1,_select);
		}
		/**
		 *增加元素 
		 * @param value
		 * @param $obj
		 * 
		 */		
		private function addElement(value:int,$obj:Object):void
		{
			
			$obj.per = value;
			_list.push($obj);
		}
		/**
		 *设置宝箱状态  
		 * 1 领取 2 完成未领取 3 未完成
		 */
		private function setBoxStatus(value:int,num:int):void
		{
			symbolSkin["treasure_" + num].filters = null;
			symbolSkin["openStatus_" + num].text = "";
			symbolSkin["btn_" + num].visible = false;
			switch(value)
			{
				case 1:
					symbolSkin["treasure_" + num].filters = filter;
					symbolSkin["openStatus_" + num].text = "已领取";
					symbolSkin["openStatus_" + num].textColor = 0x00FF00;
					symbolSkin["btn_" + num].filters = filter;
					symbolSkin["btn_" + num].visible = false;
					symbolSkin["btn_" + num].mouseEnabled = false;
					break;
				case 2:
					symbolSkin["treasure_" + num].filters = null;
					symbolSkin["openStatus_" + num].text = "";
					symbolSkin["btn_" + num].visible = true;
					symbolSkin["btn_" + num].mouseEnabled = true;
					break;
				case 3:
					symbolSkin["treasure_" + num].filters = null;
					symbolSkin["openStatus_" + num].text = "未开启";
					symbolSkin["btn_" + num].visible = false;
					break;
			}
				
		}
	}
}